package util;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @fileName: Regex
 * @author: Han
 * @date: 2018/5/24 13:19
 * @dscription:
 */
public class Regex {

    /**
     * 使用正则表达式分割句子
     *
     * @param originStr 句子
     * @param regexStr  正则表达式
     * @return 分割后的句子
     */
    static List<String> splitByRegex(String originStr, String regexStr) {

        List<String> list = new ArrayList<>();

        Pattern pattern = Pattern.compile(regexStr);
        Matcher matcher = pattern.matcher(originStr);
        int start = 0;
        int end = 0;
        while (matcher.find()) {
            end = matcher.start();
            String sentence = originStr.substring(start, end);
            start = matcher.end();
            if (!sentence.isEmpty()) {
                list.add(sentence);
            }
        }
        list.add(originStr.substring(end));
        return list;
    }

    /**
     * 采集四川政府采购项目公告
     * @param pageContent 页面源码
     * @return 各项内容
     */
    public static Map<String,String> siChuanPurchase(String pageContent){
        Map<String,String> map = new LinkedHashMap<>();
        String[] lines = pageContent.split("\n");
        String name = "-";
        String time = "-";
        String person = "-";
        String agentName = "-";
        for (int i = 0;i < lines.length;i ++){
            String line = lines[i];
            if (line.contains(">采购项目名称<")){
                line = lines[i + 1];
                int start = line.indexOf("class=\"borderttconfont\">") + 24;
                int end = line.indexOf("</td>");
                name = line.substring(start,end);
            }else if (line.contains(">公告发布时间<")){
                line = lines[i + 1];
                int start = line.indexOf("class=\"borderttconfont\">");
                if (start == -1){
                    start = line.indexOf("<td>") + 4;
                }else {
                    start += 24;
                }
                int end = line.indexOf("</td>");
                time = line.substring(start,end);
                StringBuilder temp = new StringBuilder(time);
                temp.insert(10," ");
                time = temp.toString();
            }else if (line.contains(">采购人<")){
                line = lines[i + 1];
                int start = line.indexOf("class=\"borderttconfont\">") + 24;
                int end = line.indexOf("</td>");
                person = line.substring(start,end);
            }else if (line.contains(">采购代理机构名称<")){
                line = lines[i + 1];
                int start = line.indexOf("class=\"borderttconfont\">") + 24;
                int end = line.indexOf("</td>");
                agentName = line.substring(start,end);
            }else if (line.contains("tt\">代理机构<")){
                line = lines[i + 1];
                int start = line.indexOf("class=\"borderttconfont\">") + 24;
                int end = line.indexOf("</td>");
                agentName = line.substring(start,end);
            }
        }
        map.put("采购项目名称",name);
        map.put("公告发布时间",time);
        map.put("采购人",person);
        map.put("采购代理机构名称",agentName);

        return map;
    }
}
